#include <sys/types.h>
#include <sys/socket.h>
#include <stddef.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <sys/un.h>
#include <stdio.h>

#define SPATH "server.socket"
#define PATH "client.socket"

int main(int argc, char* argv[])
{
	int cfd;
	struct sockaddr_un addr, sockaddr;
	char buf[BUFSIZ];

	cfd = socket(AF_UNIX, SOCK_STREAM, 0);

	addr.sun_family = AF_UNIX;
	strcpy(addr.sun_path, PATH);

	socklen_t len = offsetof(struct sockaddr_un, sun_path)+strlen(addr.sun_path);
	
	unlink(PATH);

	bind(cfd, (struct sockaddr*)&addr, len);
/////////////////////////////////////////////////////////////////

	sockaddr.sun_family = AF_UNIX;
	strcpy(sockaddr.sun_path, SPATH);
	len = offsetof(struct sockaddr_un, sun_path)+strlen(addr.sun_path);

	int ret = connect(cfd, (struct sockaddr*)&sockaddr, len);
	printf("connect return: %d\n", ret);

	while(1)
	{
		int ret = read(STDIN_FILENO, &buf, sizeof(buf));
		printf("ret: %d\n",ret);
		if(write(cfd, buf, ret)<0)perror("write:");

		ret = read(cfd, &buf, sizeof(buf));
		if(write(STDOUT_FILENO, buf, ret)<0)perror("write:");
	}

	close(cfd);
	return 0;
}
